var a = getApp();

module.exports = Behavior({
    data: {
        header: {},
        goodsData: [],
        isLoadMore: !0,
        page: 1,
        goodsDetail: {},
        ratio_data: {}
    },
    methods: {
        _getNetWork: function(a) {
            var t = a.url, e = a.data, o = a.header, i = a.success, s = {
                "Content-Type": "application/json",
                Accept: "application/json"
            };
            o && (s = Object.assign(s, o)), wx.showNavigationBarLoading(), wx.request({
                url: t,
                data: e,
                header: s,
                success: function(a) {
                    wx.hideNavigationBarLoading({}), i(a);
                },
                fail: function(a) {
                    wx.showToast({
                        title: "请求数据失败",
                        icon: "loading",
                        duration: 2e3,
                        mask: !0
                    }), console.log("请求数据失败:", t, a);
                }
            });
        },
        getCPSLogin: function(t) {
            var e = this;
            this.setData({
                page: 1,
                isLoadMore: !0,
                goodsData: []
            });
            var o = wx.getStorageSync("CPSHeader");
            if (o) {
                if (this.data.header = JSON.parse(o), this.data.header.t + 420 > Math.round(new Date() / 1e3)) return void ("list" === t ? this.searchCPSGood() : "good" === t ? this.getGoodDetail() : "link" === t ? this.getCPSLink() : this.getCPSGood(0, this.data.datas.list[0].text));
                wx.removeStorageSync("CPSHeader");
            }
            var i = a.getNetAddresss("plugin.aggregation-cps.api.web.mergeLogin");
            a._getNetWork({
                url: i,
                success: function(a) {
                    var o = a.data;
                    1 == o.result ? (e.setData({
                        header: o.data.header
                    }), wx.setStorageSync("CPSHeader", JSON.stringify(e.data.header)), "list" === t ? e.searchCPSGood() : "good" === t ? e.getGoodDetail() : "link" === t ? e.getCPSLink() : e.getCPSGood(0, e.data.datas.list[0].text)) : wx.showToast({
                        title: o.msg,
                        icon: "none"
                    });
                },
                fail: function(a) {
                    console.log(a, "错误");
                }
            });
        },
        getPDDLogin: function(a, t) {
            var e = this;
            this._getNetWork({
                url: "https://v3.biyingniao.com/api/v3/user/is_union_oauth",
                data: {
                    platform: 2
                },
                header: this.data.header,
                success: function(o) {
                    var i = o.data;
                    0 === i.code ? i.data.is_oauth ? "list" === a ? e.searchCPSGood() : "good" === a ? e.getGoodDetail() : e.getCPSGood(a, t) : wx.showModal({
                        title: "提示",
                        content: "请先确认授权",
                        showCancel: !1,
                        success: function(a) {
                            if (a.confirm) {
                                var t = i.data.oauth_url_info.we_app_info;
                                wx.navigateToMiniProgram({
                                    appId: t.app_id,
                                    path: t.page_path,
                                    extraData: {}
                                });
                            }
                        }
                    }) : 30004 === response.code ? e.getCPSLogin(a, t) : wx.showToast({
                        title: i.message,
                        icon: "none"
                    });
                }
            });
        },
        getCPSGood: function(a, t, e) {
            var o = this;
            if (this.data.isLoadMore) {
                this.setData({
                    isLoadMore: !1
                }), "more" !== e && this.setData({
                    show_page: !1,
                    show_text: "加载中..."
                });
                var i = "https://v3.biyingniao.com/api/v3/goods/pdd/search";
                2 == this.data.datas.list[a].is_minApp && (i = "https://v3.biyingniao.com/api/v3/goods/jd/search"), 
                this._getNetWork({
                    url: i,
                    data: {
                        keyword: t,
                        page: this.data.page
                    },
                    header: this.data.header,
                    success: function(i) {
                        var s = i.data;
                        if (0 === s.code) {
                            1 === o.data.page && o.getRatio();
                            var d = [];
                            d = "more" !== e ? s.data.data || [] : o.data.goodsData.concat(s.data.data || []), 
                            o.setData({
                                isLoadMore: s.data.has_next,
                                show_page: !0,
                                goodsData: d
                            });
                        } else 30009 === s.code || 1e4 === s.code ? (o.setData({
                            isLoadMore: !0
                        }), o.getPDDLogin(a, t, e)) : 30004 === s.code ? o.getCPSLogin(a) : (o.setData({
                            isLoadMore: !0,
                            show_page: !1,
                            show_text: "数据报错"
                        }), wx.showToast({
                            title: s.message,
                            icon: "none"
                        }));
                    }
                });
            }
        },
        searchCPSGood: function() {
            var a = this;
            if (this.data.isLoadMore) {
                this.setData({
                    isLoadMore: !1
                });
                var t = "https://v3.biyingniao.com/api/v3/goods/pdd/search";
                "jd" === this.data.searchActive && (t = "https://v3.biyingniao.com/api/v3/goods/jd/search");
                var e = {
                    keyword: "jd" === this.data.searchActive ? this.data.searchKey || "百货" : this.data.searchKey,
                    page: this.data.page
                };
                !this.data.searchKey && this.data.channel_type && (e = {
                    channel_type: this.data.channel_type,
                    page: this.data.page
                }, t = "https://v3.biyingniao.com/api/v3/goods/pdd/recommend", "jd" === this.data.searchActive && (t = "https://v3.biyingniao.com/api/v3/goods/jd/jf")), 
                this.data.searchKey ? this.data.channel_type_name = "搜索" : this.data.options.channel_type_name && (this.data.channel_type_name = "".concat(this.data.options.channel_type_name).concat("jd" === this.data.searchActive ? "-京东" : "-拼多多") || "商品列表"), 
                wx.setNavigationBarTitle({
                    title: this.data.channel_type_name
                }), this._getNetWork({
                    url: t,
                    data: e,
                    header: this.data.header,
                    success: function(t) {
                        var e = t.data;
                        0 === e.code ? (1 === a.data.page && a.getRatio(), a.setData({
                            isLoadMore: e.data.has_next,
                            goodsData: a.data.goodsData.concat(e.data.data || []),
                            page: a.data.page += 1
                        })) : 30009 === e.code || 1e4 === e.code ? (a.setData({
                            isLoadMore: !0
                        }), a.getPDDLogin("list")) : 30004 === e.code ? a.getCPSLogin("list") : (a.setData({
                            isLoadMore: !0
                        }), wx.showToast({
                            title: e.message,
                            icon: "none"
                        }));
                    }
                });
            }
        },
        getGoodDetail: function() {
            var a = this;
            this._getNetWork({
                url: "https://v3.biyingniao.com/api/v3/goods/detail",
                data: {
                    item_id: this.data.good.item_id,
                    platform_id: this.data.good.platform_id,
                    activity_id: this.data.good.activity_id
                },
                header: this.data.header,
                success: function(t) {
                    var e = t.data;
                    0 === e.code ? (a.setData({
                        goodsDetail: e.data || {}
                    }), a.getRatio()) : 30009 === e.code || 1e4 === e.code ? a.getPDDLogin("good") : 30004 === e.code ? a.getCPSLogin("good") : wx.showToast({
                        title: e.message,
                        icon: "none"
                    });
                }
            });
        },
        getCPSLink: function() {
            this._getNetWork({
                url: "https://v3.biyingniao.com/api/v3/popularize/cps",
                data: {
                    id: this.data.id,
                    phone: this.data.phone
                },
                header: this.data.header,
                success: function(a) {
                    var t = a.data;
                    0 === t.code ? wx.navigateToMiniProgram({
                        appId: t.data.wx_app_id,
                        path: t.data.wx_path,
                        extraData: {}
                    }) : wx.showToast({
                        title: t.message,
                        icon: "none"
                    });
                }
            });
        },
        getRatio: function() {
            var t = this;
            if (!this.data.ratio_data || !this.data.ratio_data.ratio) {
                var e = a.getNetAddresss("plugin.aggregation-cps.api.setting.index");
                a._getNetWork({
                    url: e,
                    success: function(a) {
                        var e = a.data;
                        1 == e.result ? t.setData({
                            ratio_data: e.data,
                            ratioCommissionAmount: (t.data.goodsDetail.discount_price * (t.data.goodsDetail.tk_rate / 100) * .78 * .97 * (e.data.ratio / 100)).toFixed(2),
                            shareCommissionAmount: Number((t.data.goodsDetail.discount_price * (t.data.goodsDetail.tk_rate / 100) * .78 * .97 * (e.data.agent_ratio / 100)).toFixed(2))
                        }) : wx.showToast({
                            title: e.msg,
                            icon: "none"
                        });
                    },
                    fail: function(a) {
                        console.log(a, "错误");
                    }
                });
            }
        }
    }
});